package services

import (
	"context"
	"github.com/golang/protobuf/ptypes/empty"
	"github.com/golang/protobuf/ptypes/wrappers"

	context2 "kop/framework/context"
	"kop/framework/errors"
	"kop/modules/award/models"
	"kop/modules/quest/configuration/dailyquest/type"
	"kop/pb"

	guildC "kop/modules/guild/client"
	moneyC "kop/modules/money/client"
	questC "kop/modules/quest/client"
	roleC "kop/modules/role/client"
)

type RoleService struct {
	context2.Context
}

//
func (s *RoleService) GetRole(ctx context.Context, req *wrappers.Int32Value) (*pb.RoleReply, error) {

	var reply = pb.RoleReply{
		Role:        roleC.GetRole(req.Value),
		Declaration: roleC.GetDeclaration(req.Value),
	}

	var guild, err = guildC.MyGuild(req.Value, req.Value, 0)
	if err == nil && guild != nil {
		reply.GuildName = guild.Name
	}

	return &reply, nil
}

// 获取当前主线关卡
//func (s *RoleService) GetStage(ctx context.Context, _ *empty.Empty) (*pb.Stage, error) {
//
//	var roleID = s.GetRoleID(ctx)
//	return client.GetStage(roleID), nil
//}

// 升级爵位
func (s *RoleService) LevelUp(ctx context.Context, req *empty.Empty) (*pb.LevelUpResp, error) {

	return roleC.RoleService.LevelUp(ctx, req)
}

// 领取每日奖励
//func (s *RoleService) DailyDiamond(ctx context.Context, e *empty.Empty) (*pb.DailyDiamondReply, error) {
//
//	var reply pb.DailyDiamondReply
//	var roleID = s.GetRoleID(ctx)
//
//	if !roleC.IsDailyDiamond(roleID) {
//		return &reply, errors.AlreadyReceiveError
//	}
//
//	var role = roleC.GetRole(roleID)
//	var conf, err = roleConf.GetByRoleLevel(role.Level)
//	if err != nil {
//		return &reply, err
//	}
//
//	roleC.UpdateDailyDiamond(roleID)
//
//	reply.Item = itemC.AddItem(roleID, consts.Diamond, conf.AwardDiamond)
//	return &reply, nil
//}

func (s *RoleService) SetDeclaration(ctx context.Context, req *wrappers.StringValue) (*empty.Empty, error) {

	return roleC.RoleService.SetDeclaration(ctx, req)
}

func (s *RoleService) FinishQuest(ctx context.Context, req *empty.Empty) (*pb.FinishQuestReply, error) {

	return questC.QuestService.Finish(ctx, req)
}

func (s *RoleService) FinishDailyQuest(ctx context.Context, req *wrappers.Int32Value) (*pb.FinishDailyQuestReply, error) {

	var reply pb.FinishDailyQuestReply
	var roleID = s.GetRoleID(ctx)

	var dailyQuests = questC.GetDailyQuests(roleID)

	var dailyQuest *pb.DailyQuest

	for _, v := range dailyQuests {
		if v.ID == req.Value {
			dailyQuest = v
		}
	}

	if dailyQuest == nil {
		return &reply, errors.ParamError
	}

	if dailyQuest.Value < dailyQuest.NeedValue {
		return &reply, errors.ParamError
	}

	var role = roleC.GetRole(roleID)
	reply.Award = models.RandAward(role, dailyQuest.AwardID)

	if dailyQuest.Finish {
		return &reply, nil
	}

	// 先把请求的任务完成
	dailyQuest.Finish = true
	questC.SaveDailyQuest(dailyQuest)

	reply.DailyQuests = []*pb.DailyQuest{dailyQuest}

	// 给特殊的日常增加活跃度值
	for _, v := range dailyQuests {
		if _type.Type(v.Type) == _type.FinishDailyQuest && !v.Finish {

			v.Value += int64(dailyQuest.DailyQuestValue)
			questC.SaveDailyQuest(v)

			reply.DailyQuests = append(reply.DailyQuests, v)
		}
	}

	// 最后发奖励
	return &reply, models.SendAward(role, reply.Award)
}

// 全员出击
func (s *RoleService) UseRum(ctx context.Context, req *empty.Empty) (*pb.UseRumResp, error) {

	return moneyC.MoneyService.UseRum(ctx, req)
}

func (s *RoleService) SignIn(ctx context.Context, req *empty.Empty) (*pb.Award, error) {

	return roleC.RoleService.SignIn(ctx, req)
}

func (s *RoleService) GetDay7Awards(ctx context.Context, req *wrappers.Int32Value) (*pb.GetDay7AwardsResp, error) {

	return roleC.RoleService.GetDay7Awards(ctx, req)
}

func (s *RoleService) GetFirstPayAwards(ctx context.Context, req *empty.Empty) (*pb.FirstPayResp, error) {

	return roleC.RoleService.GetFirstPayAwards(ctx, req)
}

func (*RoleService) SetName(ctx context.Context, req *pb.SetNameRequest) (*wrappers.Int32Value, error) {

	return roleC.RoleService.SetName(ctx, req)
}

func (*RoleService) SetProgress(ctx context.Context, req *wrappers.StringValue) (*empty.Empty, error) {

	return roleC.RoleService.SetProgress(ctx, req)
}
